using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The FamilyMemberProfileTableDB class is responsible for interacting with the database to retrieve and store information
/// about FamilyMemberProfileTable objects.
/// </summary>
public class FamilyMemberProfileTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of FamilyMemberProfileTable from the underlying datasource.
/// </summary>
/// <param name="familyMemberProfileKey">The unique FamilyMemberProfileKey of the FamilyMemberProfileTable in the database.</param>
/// <returns>An FamilyMemberProfileTable when the FamilyMemberProfileKey was found in the database, or null otherwise.</returns>
public static FamilyMemberProfileTable GetItem(int familyMemberProfileKey)
{
FamilyMemberProfileTable myFamilyMemberProfileTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@familyMemberProfileKey", familyMemberProfileKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myFamilyMemberProfileTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myFamilyMemberProfileTable;
}
}

public static FamilyMemberProfileTable GetItemByUserAccountKey(int UserAccountKey)
{
    FamilyMemberProfileTable myFamilyMemberProfileTable = null;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableSelectSingleItemByUserAccountKey", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.Read())
                {
                    myFamilyMemberProfileTable = FillDataRecord(myReader);
                }
                myReader.Close();
            }
            myConnection.Close();
        }
        return myFamilyMemberProfileTable;
    }
}

/// <summary>
/// Returns a list with FamilyMemberProfileTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberProfileTable objects.</returns>
public static FamilyMemberProfileTableList GetList()
{
FamilyMemberProfileTableList tempList = new FamilyMemberProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with FamilyMemberProfileTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberProfileTable objects.</returns>
public static FamilyMemberProfileTableList GetListByBloodGroupCode(short BloodGroupCode)
{
FamilyMemberProfileTableList tempList = new FamilyMemberProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableSelectListByBloodGroupCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@bloodGroupCode", BloodGroupCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with FamilyMemberProfileTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberProfileTable objects.</returns>
public static FamilyMemberProfileTableList GetListByMartialStatusCode(short MartialStatusCode)
{
FamilyMemberProfileTableList tempList = new FamilyMemberProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableSelectListByMartialStatusCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@martialStatusCode", MartialStatusCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with FamilyMemberProfileTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberProfileTable objects.</returns>
public static FamilyMemberProfileTableList GetListByNationalityCode(short NationalityCode)
{
FamilyMemberProfileTableList tempList = new FamilyMemberProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableSelectListByNationalityCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@nationalityCode", NationalityCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with FamilyMemberProfileTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberProfileTable objects.</returns>
public static FamilyMemberProfileTableList GetListByRaceCode(short RaceCode)
{
FamilyMemberProfileTableList tempList = new FamilyMemberProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableSelectListByRaceCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@raceCode", RaceCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with FamilyMemberProfileTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberProfileTable objects.</returns>
public static FamilyMemberProfileTableList GetListByReligionCode(short ReligionCode)
{
FamilyMemberProfileTableList tempList = new FamilyMemberProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableSelectListByReligionCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@religionCode", ReligionCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with FamilyMemberProfileTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberProfileTable objects.</returns>
public static FamilyMemberProfileTableList GetListByUserAccountKey(int UserAccountKey)
{
FamilyMemberProfileTableList tempList = new FamilyMemberProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a FamilyMemberProfileTable in the database.
/// </summary>
/// <param name="myFamilyMemberProfileTable">The FamilyMemberProfileTable instance to save.</param>
/// <returns>The new FamilyMemberProfileKey if the FamilyMemberProfileTable is new in the database or the existing FamilyMemberProfileKey when an item was updated.</returns>
public static int Save(FamilyMemberProfileTable myFamilyMemberProfileTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myFamilyMemberProfileTable.FamilyMemberProfileKey == -1)
{myCommand.Parameters.AddWithValue("@familyMemberProfileKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@familyMemberProfileKey", myFamilyMemberProfileTable.FamilyMemberProfileKey);
}
if (myFamilyMemberProfileTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myFamilyMemberProfileTable.UserAccountKey);
}
if (string.IsNullOrEmpty(myFamilyMemberProfileTable.FirstName))
{
myCommand.Parameters.AddWithValue("@firstName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@firstName", myFamilyMemberProfileTable.FirstName);
}
if (string.IsNullOrEmpty(myFamilyMemberProfileTable.LastName))
{
myCommand.Parameters.AddWithValue("@lastName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@lastName", myFamilyMemberProfileTable.LastName);
}
if (string.IsNullOrEmpty(myFamilyMemberProfileTable.OtherName))
{
myCommand.Parameters.AddWithValue("@otherName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@otherName", myFamilyMemberProfileTable.OtherName);
}
if (myFamilyMemberProfileTable.DateOfBirth == null){
myCommand.Parameters.AddWithValue("@dateOfBirth", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@dateOfBirth", myFamilyMemberProfileTable.DateOfBirth);
}
if (myFamilyMemberProfileTable.BloodGroupCode == null){
myCommand.Parameters.AddWithValue("@bloodGroupCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@bloodGroupCode", myFamilyMemberProfileTable.BloodGroupCode);
}
if (myFamilyMemberProfileTable.MartialStatusCode == null){
myCommand.Parameters.AddWithValue("@martialStatusCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@martialStatusCode", myFamilyMemberProfileTable.MartialStatusCode);
}
if (myFamilyMemberProfileTable.RaceCode == null){
myCommand.Parameters.AddWithValue("@raceCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@raceCode", myFamilyMemberProfileTable.RaceCode);
}
if (myFamilyMemberProfileTable.ReligionCode == null){
myCommand.Parameters.AddWithValue("@religionCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@religionCode", myFamilyMemberProfileTable.ReligionCode);
}
if (myFamilyMemberProfileTable.NationalityCode == null){
myCommand.Parameters.AddWithValue("@nationalityCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@nationalityCode", myFamilyMemberProfileTable.NationalityCode);
}
if (string.IsNullOrEmpty(myFamilyMemberProfileTable.NRICNumber))
{
myCommand.Parameters.AddWithValue("@nRICNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@nRICNumber", myFamilyMemberProfileTable.NRICNumber);
}
if (myFamilyMemberProfileTable.DrivingLicenceType == null){
myCommand.Parameters.AddWithValue("@drivingLicenceType", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@drivingLicenceType", myFamilyMemberProfileTable.DrivingLicenceType);
}
if (string.IsNullOrEmpty(myFamilyMemberProfileTable.DrivingLicenseNumber))
{
myCommand.Parameters.AddWithValue("@drivingLicenseNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@drivingLicenseNumber", myFamilyMemberProfileTable.DrivingLicenseNumber);
}
if (myFamilyMemberProfileTable.TravelDocumentType == null){
myCommand.Parameters.AddWithValue("@travelDocumentType", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@travelDocumentType", myFamilyMemberProfileTable.TravelDocumentType);
}
if (string.IsNullOrEmpty(myFamilyMemberProfileTable.TravelDocumentNumber))
{
myCommand.Parameters.AddWithValue("@travelDocumentNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@travelDocumentNumber", myFamilyMemberProfileTable.TravelDocumentNumber);
}
if (string.IsNullOrEmpty(myFamilyMemberProfileTable.WebSiteUrl))
{
myCommand.Parameters.AddWithValue("@webSiteUrl", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@webSiteUrl", myFamilyMemberProfileTable.WebSiteUrl);
}
if (myFamilyMemberProfileTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myFamilyMemberProfileTable.Status);
}
if (myFamilyMemberProfileTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myFamilyMemberProfileTable.CreatedDateTime);
}
if (myFamilyMemberProfileTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myFamilyMemberProfileTable.ModifiedDateTime);
}

if (myFamilyMemberProfileTable.Gender == null)
{
    myCommand.Parameters.AddWithValue("@Gender", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@Gender", myFamilyMemberProfileTable.Gender);
}


DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a FamilyMemberProfileTable from the database.
/// </summary>
/// <param name="familyMemberProfileKey">The FamilyMemberProfileKey of the FamilyMemberProfileTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int familyMemberProfileKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberProfileTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@familyMemberProfileKey", familyMemberProfileKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the FamilyMemberProfileTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static FamilyMemberProfileTable FillDataRecord(IDataRecord myDataRecord )
{
FamilyMemberProfileTable myFamilyMemberProfileTable = new FamilyMemberProfileTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FamilyMemberProfileKey")))
{
myFamilyMemberProfileTable.FamilyMemberProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("FamilyMemberProfileKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myFamilyMemberProfileTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FirstName")))
{
myFamilyMemberProfileTable.FirstName = myDataRecord.GetString(myDataRecord.GetOrdinal("FirstName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LastName")))
{
myFamilyMemberProfileTable.LastName = myDataRecord.GetString(myDataRecord.GetOrdinal("LastName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherName")))
{
myFamilyMemberProfileTable.OtherName = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DateOfBirth")))
{
myFamilyMemberProfileTable.DateOfBirth = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("DateOfBirth"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BloodGroupCode")))
{
myFamilyMemberProfileTable.BloodGroupCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("BloodGroupCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MartialStatusCode")))
{
myFamilyMemberProfileTable.MartialStatusCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("MartialStatusCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RaceCode")))
{
myFamilyMemberProfileTable.RaceCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("RaceCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ReligionCode")))
{
myFamilyMemberProfileTable.ReligionCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("ReligionCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("NationalityCode")))
{
myFamilyMemberProfileTable.NationalityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("NationalityCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("NRICNumber")))
{
myFamilyMemberProfileTable.NRICNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("NRICNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DrivingLicenceType")))
{
myFamilyMemberProfileTable.DrivingLicenceType = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DrivingLicenceType"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DrivingLicenseNumber")))
{
myFamilyMemberProfileTable.DrivingLicenseNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("DrivingLicenseNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TravelDocumentType")))
{
myFamilyMemberProfileTable.TravelDocumentType = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TravelDocumentType"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TravelDocumentNumber")))
{
myFamilyMemberProfileTable.TravelDocumentNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("TravelDocumentNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("WebSiteUrl")))
{
myFamilyMemberProfileTable.WebSiteUrl = myDataRecord.GetString(myDataRecord.GetOrdinal("WebSiteUrl"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myFamilyMemberProfileTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myFamilyMemberProfileTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myFamilyMemberProfileTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Gender")))
{
    myFamilyMemberProfileTable.Gender = myDataRecord.GetString(myDataRecord.GetOrdinal("Gender"));
}
return myFamilyMemberProfileTable;
}
}

 } 
